<?php
/**
 * Created by PhpStorm.
 * User: 36980
 * Date: 2017/08/12
 * Time: 14:28
 */

namespace Admin\Controller;

use Think\Controller;


class UrlAnalysisController extends BaseController
{
    /***
     * 程序入口 主要是做页面展示用
     * @param string $key
     */

    public function index()
    {


        $tag = $_GET['tag'];
        $key = $_POST['key'];

        /*  if(empty($tag)){
              $this->display("Index/index");
          }*/
        //调用查询方法
        switch ($tag) {
            case "a":
                $url = "UrlAnalysis/indexa";
                break;
            case "b":
                $url = "UrlAnalysis/index";
                break;
            case "c":
                $url = "UrlAnalysis/indexc";
                break;
        }
        UrlAnalysisController::select($tag, $key);

        $this->display($url);
    }

    //添加b页面
    public function insert()
    {
        $tag = $_GET['tag'];
        //默认显示添加表单
        if (!IS_POST) {
            if ($tag == 'c') {
                $model = M("Classaurl")->where()->select();
                $this->assign('member', $model);
            }
            $this->assign('tag', $_GET['tag']);

            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $type = $_POST['type'];
            if ($tag == "a") {
                if (!trim(I('post.caURL'))) {
                    $this->error('地址不能为空');
                    exit;
                }
                if (!trim(I('post.caName'))) {
                    $this->error('名称不能为空');
                    exit;
                }
                $model = M("Classaurl");
                //获取用户输入的信息.
                $data["caAddTime"] = date("Y-m-d H:i:s");
                $data["caName"] = $_POST['caName'];
                $data["caURL"] = $_POST['caURL'];
                $data["caRemarks"] = $_POST['caRemarks'];

                if (isset($_POST["levelURL"])) {
                    $data["levelURL"] = $_POST["levelURL"];
                    $model->where('1=1')->setField('levelURL', 1);

                } else {
                    $data["levelURL"] = 1;
                }

            } else {
                if ($tag == "c") {
					  if (!trim(I('post.ccAnalysis'))) {
                    $this->error('地址不能为空');
                    exit;
                }
                    $model = M("Classcurl");
                    $data["ccAddTime"] = date("Y-m-d H:i:s");
                    $data["ccAnalysis"] = $_POST['ccAnalysis'];
                    $data["caId"] = $_POST['ccOriginal'];
                   // $caid = $_POST['ccOriginal'];
                   // $list = M("Classaurl")->where("caId =$caid")->find();
                   // $data["ccOriginal"] = $list["caurl"];
                } else {
                    $model = M("Classburl");
                    //获取用户输入的信息.
                    $data["cbAddTime"] = date("Y-m-d H:i:s");
                    $data["cbName"] = $_POST['cbName'];
                    $data["cbURL"] = $_POST['cbURL'];
                    $data["cbRemarks"] = $_POST['cbRemarks'];
                }


            }


            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {

                if ($cid = $model->data($data)->add()) {
                    if ($tag == 'b') {
                        $where['cbId'] = $cid;

                        //添加条件
                        $root = $_SERVER['HTTP_HOST'] . __ROOT__ . "/Extension/index.html?jump=$cid";
                        $data = array("cbExtension" => $root, "cbAddTime" => date("Y-m-d H:i:s"));
                        $model->where($where)->setField($data);
                        $this->success("添加成功", U('UrlAnalysis/index', array('tag' => 'b')));
                    } else if ($tag == 'a') {

                        $this->success("添加成功", U('UrlAnalysis/index', array('tag' => 'a')));
                    } else if ($tag == 'c') {
                        $this->success("添加成功", U('UrlAnalysis/index', array('tag' => 'c')));
                    }

                } else {
                    $this->error("添加失败");
                }
            }
        }
    }

//修改a
    public function updatea()
    {
        if (!IS_POST) {
            $id = I('id');
            $model = M('Classaurl')->where("caId = $id")->find();
            $this->assign('post', $model);
            $this->display();
        }
        if (IS_POST) {
            $model = M("Classaurl");
            $id = $_POST['caId'];
            $data["caName"] = $_POST['caName'];
            $data["caURL"] = $_POST['caURL'];
            $data["caRemarks"] = $_POST['caRemarks'];
            $data["levelURL"] = $_POST['levelURL'];
            if (isset($_POST['levelURL'])) {
                $data["levelURL"] = $_POST['levelURL'];
                $model->where('1=1')->setField('levelURL', 1);
            } else {
                $data["levelURL"] = 1;
            }
            if (!$model->create()) {
                $this->error($model->getError());
            } else {

                if ($model->where("caId = $id")->data($data)->save()) {
                    $where['caId'] = $id;

                    $data = array("caUpdateTime" => date("Y-m-d H:i:s"));
                    $model->where($where)->setField($data);
                    $this->success("更新成功", U('UrlAnalysis/index/tag/a'));
                } else {
                    $this->error("更新失败或填入数据没有变动");
                }
            }

        }

    }

//修改b
    public function update()
    {
        if (!IS_POST) {
            $id = I('id');
            $model = M('Classburl')->where("cbId = $id")->find();
            $this->assign('post', $model);
            $this->display();
        }
        if (IS_POST) {
            $model = M("Classburl");
            $id = $_POST['cbId'];
            if (!$model->create()) {
                $this->error($model->getError());
            } else {

                if ($model->save()) {
                    $where['cbId'] = $id;

                    $lists = M('Classaurl')->where('caState = 0 and levelURL =0')->find();
                    $res = "/Extension/jump.html?jump=$id";
                    // $shorturl = urlShort($res);

                    if (!empty($lists)) {
                        $root = $lists['caurl'] . __ROOT__ . "$res";
                    } else {
                        $root = $_SERVER['HTTP_HOST'] . __ROOT__ . "$res";
                    }
                    $data = array("cbExtension" => $root, "cbUpdateTime" => date("Y-m-d H:i:s"));
                    $model->where($where)->setField($data);
                    $this->success("更新成功", U('UrlAnalysis/index/tag/b'));
                } else {
                    $this->error("更新失败或填入数据没有变动");
                }
            }

        }
    }

    public function updatec()
    {
        if (!IS_POST) {
            $id = I('id');
            $model = M('Classcurl')->where("ccId = $id")->find();
            $member = M("Classaurl")->where()->select();
            $this->assign('member', $member);
            $this->assign('post', $model);
            $this->display();
        }
        if (IS_POST) {
            $model = M("Classcurl");
            $id = $_POST['ccId'];
            if (!$model->create()) {
                $this->error($model->getError());
            } else {

                if ($model->save()) {
                    $where['ccId'] = $id;

                    $data = array("ccUpdateTime" => date("Y-m-d H:i:s"));
                    $model->where($where)->setField($data);
                    $this->success("更新成功", U('UrlAnalysis/index/tag/c'));
                } else {
                    $this->error("更新失败或填入数据没有变动");
                }
            }

        }
    }

    public function delete()
    {
        $id = I(trim('get.id'));
        $tag = I(trim('get.tag'));

        switch ($tag){
            case 'a':
                $model= M('Classaurl')->where("caId = $id")->delete();
                if ($model){
                    $this->success("删除成功", U('UrlAnalysis/index/tag/a'));
                }else{
                    $this->error("删除失败");
                }
                break;
            case 'b':
                $model= M('Classburl')->where("cbId = $id")->delete();
                if ($model){
                    $this->success("删除成功", U('UrlAnalysis/index/tag/b'));
                }else{
                    $this->error("删除失败");
                }
                break;
            case 'c':
                $model= M('Classcurl')->where("ccId = $id")->delete();
                if ($model){
                    $this->success("删除成功", U('UrlAnalysis/index/tag/c'));
                }else{
                    $this->error("删除失败");
                }
                break;

        }
    }


    // 查询
    public function select($tag, $key)
    {
        //定义一个变量来存储排序规则
        $sort = "";
        //判断属于哪个表内容
        switch ($tag) {
            case "a":
                $sort = "caId DESC";
                if ($key == "") {
                    $model = M('classaurl');
                } else {
                    //模糊查询
                    $where['caName'] = array('like', '%' . trim($key) . '%');
                    $model = M('classaurl')->where($where);
                }
                break;
            case "b":
                $sort = "cbId DESC";

                if ($key == "") {
                    $model = M('classburl');
                } else {
                    //模糊查询
                    $where['cbName'] = array('like', '%' . trim($key) . '%');
                    $model = M('classburl')->where($where);
                }
                break;
            case "c":
                $sort = "ccId DESC";
                if ($key == "") {
                    $model = M('classcurl');
                } else {
                    //模糊查询
                    $where['ccAnalysis'] = array('like', '%' . trim($key) . '%');
                    $model = M('classcurl')->where($where);
                }
                break;
        }

        $count = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member = $model->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->order($sort)->select();
        $this->assign('member', $member);
        $this->assign('page', $show);//

    }

}